package com.rd.student.jiaji.class2.shirui.stuentmanagemensystem;

import java.util.*;

public class Student  {
    private String name;
    private String major;
    private String className;
    private int age;
    private String describe;
    private int studentID;
    private static int studentCount = 7;

    public Student() {

    }

    public Student(String name, String major, String className, int age, String describe, int studentID) {
        this.name = name;
        this.major = major;
        this.className = className;
        this.age = age;
        this.describe = describe;
        this.studentID = studentID;
    }


    public String getName() {
        return name;
    }

    public String getMajor() {
        return major;
    }

    public String getClassName() {
        return className;
    }

    public int getAge() {
        return age;
    }

    public int getStudentID() {
        return studentID;
    }

    //功能1:新增小组成员
    public List addStudent(List studentList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入添新学生的姓名信息：");
        System.out.print("姓名：");
        String name = scanner.nextLine();

        // 检查名字是否重复
        for (Object obj : studentList) {
            Student student = (Student) obj;
            if (student.getName().equals(name)) {
                System.out.println("插入失败，新成员名字重复。");
                return studentList;
            }
        }
        System.out.print("班级：" + "例如:Class 2");
        String className = scanner.nextLine();
        System.out.print("专业：");
        String major = scanner.nextLine();
        System.out.print("年龄：");
        int age = Integer.parseInt(scanner.nextLine());
        System.out.print("描述：");
        String describe  = scanner.nextLine();
        // 自动生成学号
        int studentId = studentCount + 1;
        studentCount++;
        // 创建新成员
        Student newStudent = new Student(name, major, className, age, describe, studentId);
        // 添加到成员列表中
        studentList.add(newStudent);
        System.out.println("插入成功。");
        return studentList;
    }

    //功能2：移除小组成员
    public List removeStudent(List studentList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要移除的成员信息：");
        System.out.print("姓名：");
        String input = scanner.nextLine();
        //用于标记是否找到了要移除的学生
        boolean found = false;
        Iterator iterator = studentList.iterator();
        while (iterator.hasNext()) {
            // 迭代器返回下一个元素，并将其转换为 Student 类型
            Student student = (Student) iterator.next();
            // 检查名字是否存在
            if (student.getName().equals(input)) {
                found = true;
                // 检查班级的人数是否为1
                if (checkClass(student, studentList)) {
                    System.out.println("暂时无法移除该成员，其所在班级只有一人。");
                    return studentList;
                }
                iterator.remove();
                System.out.println("成功移除成员：" + student.getName());
                break;
            }
        }

        if (!found) {
            System.out.println("处理失败，该成员不存在。");
        }
        return studentList;
    }

    //判断班级剩人数是否为1的方法
    public boolean checkClass(Student student, List studentList) {
        int count = 0;
        for (Object obj : studentList) {
            Student student1 = (Student) obj;
            // 判断当前学生对象的班级名称是否与传入的学生对象相同
            if (student1.getClassName().equals(student.getClassName())) {
                // 如果班级名称相同，则表明有一名学生存在，count++
                count++;
            }
        }

        return count == 1;
    }

    //功能3: 根据班级查询人数
    public int getClassStudent(List studentList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("根据班级查询成员列表:");
        System.out.println("输入班级： 例如:Class 1");
        String inputName = scanner.nextLine();
        String className1 = inputName;
        int count = 0;
        for (Object obj : studentList) {
            Student student = (Student) obj;
            if (student.getClassName().equals(className1)) {
                count++;
            }
        }
        System.out.println("班级: " + className1 + ", 人数: " + count);
        return count;
    }

    //功能4:  根据专业查询人数
    public int getMajorStudent(List studentList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("根据专业查询成员列表:");
        System.out.println("输入专业： ");
        String inputMajor = scanner.nextLine();
        String major1 = inputMajor;
        int count = 0;
        for (Object obj : studentList) {
            Student student = (Student) obj;
            if (student.getMajor().equals(major1)) {
                count++;
            }
        }
        System.out.println("专业: " + major1 + ", 人数: " + count);
        return count;
    }

    //功能5: 根据年龄查询人数
    public int getAgeStudent(List studentList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("根据年龄查询成员列表:");
        System.out.println("输入年龄：");
        int inputAge = Integer.parseInt(scanner.nextLine());
        int age1 = inputAge;
        int count = 0;
        for (Object obj : studentList) {
            Student student = (Student) obj;
            if (student.getAge() == age1) {
                count++;
            }
        }
        System.out.println("年龄: " + age1 + ", 人数: " + count);
        return count;

    }

    //功能6:  返回所有成员名字
    public void getAllName(List studentList) {
        for (Object obj : studentList) {
            Student student = (Student) obj;
            System.out.print("< ");
            System.out.print(student.getName());
            System.out.print(" >");
        }
    }

    //功能7: 计算平均年龄
    public double averAge(List studentList) {
        int sum = 0;
        for (Object obj : studentList) {
            Student student = (Student) obj;
            sum += student.getAge();
        }
        System.out.println("该组成员的平均年龄为" + sum / studentList.size());
        return (double) sum / studentList.size();
    }

    //功能8:  降序排序
    public List downOrder(List studentList) {
        // 创建新的list，用studentlist初始化
        List sortedList = new LinkedList(studentList);
        // 匿名内部类作为比较器对象
        Collections.sort(sortedList, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                // 按照学生年龄降序排列
                if (s2.getAge() > s1.getAge()) {
                    return 1;  // 返回正数表示降序
                } else if (s2.getAge() < s1.getAge()) {
                    return -1;   // 返回负数表示升序
                } else {
                    return 0;       // 返回零表示相等
                }
            }
        });
        // 创建新的list保存排序后的数据
        List result = new ArrayList();
        for (Object obj : sortedList) {
            Student student = (Student) obj;
            result.add("姓名：" + student.getName() + "，年龄：" + student.getAge());
        }
        System.out.println(result);
        return result;
    }


    //功能9:升序排序
    public List upOrder(List studentList) {
        // 创建新的list，用studentlist初始化
        List sortedList = new LinkedList(studentList);
        // 匿名内部类作为比较器对象
        Collections.sort(sortedList, new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                // 按照学生年龄降序排列
                if (s2.getAge() > s1.getAge()) {
                    return -1;  // 返回负数数表示降序
                } else if (s2.getAge() < s1.getAge()) {
                    return 1;   // 返回正数表示升序
                } else {
                    return 0;       // 返回零表示相等
                }
            }
        });
        // 创建新的list保存排序后的数据
        List result = new ArrayList();
        for (Object obj : sortedList) {
            Student student = (Student) obj;
            result.add("姓名：" + student.getName() + "，年龄：" + student.getAge());
        }
        System.out.println(result);
        return result;
    }

    //功能10: 返回指定年龄范围的成员
    public List getLimitAge(List studentList) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入查询年龄的最小值");
        int minage = scanner.nextInt();
        System.out.println("输入查询年龄的最大值");
        int maxage = scanner.nextInt();

        for (Object obj : studentList) {
            Student student = (Student) obj;
            {
                if (student.getAge() >= minage && student.getAge() <= maxage) {
                    System.out.println(student);
                }
            }

        }
        return studentList;
    }

    //功能11: 输功能菜单
    public void printMenu() {
        System.out.println("------欢迎进入学生管理系统----------");
        System.out.println("------输入编号可以选择对应功能------");
        Scanner scanner = new Scanner(System.in);
        System.out.println("1.新增小组成员");
        System.out.println("2.移除小组成员");
        System.out.println("3.根据班级查询成员列表");
        System.out.println("4.根据专业查询成员列表");
        System.out.println("5.根据年龄查询成员列表");
        System.out.println("6.返回所有成员名字");
        System.out.println("7.计算成员平均年龄");
        System.out.println("8.降序年龄排序成员");
        System.out.println("9.升序年龄排序成员");
        System.out.println("10.根据年龄范围返回成员列表");
        System.out.println("11.查看功能菜单");
        System.out.println("12.刷新查看所有成员信息");
        System.out.println("---------------------------------");
    }


    //功能12: 刷新成员列表
    public List printStudent(List studentList) {
        for (Object obj : studentList) {
            Student student = (Student) obj;
            System.out.println(student);
        }
        return studentList;
    }


    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", major='" + major + '\'' +
                ", className='" + className + '\'' +
                ", age=" + age +
                ", describe='" + describe + '\'' +
                ", studentID=" + studentID +
                '}';
    }


}
